package org.jha.card.mwrf;

import com.sun.jna.ptr.LongByReference;

public class MwfCardDemo {
	public static void main(String[] args) {
		Mwrf32 lib = Mwrf32.INSTANCE;
		int icdev = lib.rf_init(0, 9600);
		System.out.println("icdev:" + icdev);
		if (icdev < 0) {
			System.out.println("connect failed!!");
			return;
		}

		// System.out.println(lib.rf_beep(icdev, 10));

		for (int sector = 0; sector < 16; sector++) {
			lib.rf_load_key_hex(icdev, 0, sector, "ffffffffffff");
		}
		LongByReference snr = new LongByReference();
		lib.rf_card(icdev, 1, snr);
		//
		System.out.println("cardno:" + snr.getPointer().getLong(0));
		System.out.println("cardno(hex):"
				+ Long.toHexString(snr.getPointer().getLong(0)).toUpperCase());

		int sector = 8;
		int rf_authentication = lib.rf_authentication(icdev, 0, sector);
		if (rf_authentication == 1) {
			System.out.println("authentication failed; check password!");
			return;
		}
		System.out.println("authentication:" + rf_authentication);

		int pos = sector * 4 + 1;// sector 8
									// p1(second,3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x3x)
		//
		lib.rf_write(icdev, pos, "xxxxxxxxxxxxxxxx");

		byte[] data = new byte[16];
		lib.rf_read(icdev, pos, data);
		lib.rf_halt(icdev);

		System.out.println(new String(data));

	}
}
